﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kuree.Sims3
{
    public class ViewItem
    {
        protected string mPath;
        private bool mEnabled = true;
        /// <summary>
        /// The name of the view item
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// The instance of the view item
        /// </summary>
        public ulong Instance { get; set; }
        /// <summary>
        /// The type flag of the item
        /// </summary>
        public TypeFlag Flag { get; set; }
        /// <summary>
        /// The image stream of the view item
        /// </summary>
        public Stream ImageStream { get; set; }
        /// <summary>
        /// The path of the view item
        /// </summary>
        public string Path { get { return mPath; } }
        public bool IsCorrupted { get; set; }
        /// <summary>
        /// Indicates the status of the specific item defined in the path. True to normal package and false
        /// to "vdisabled" package
        /// </summary>
        public bool Enabled
        {
            get
            {
                return mEnabled;
            }
            set
            {
                switch (value)
                {
                    case true:
                        if (!this.mEnabled)
                        {
                            if (System.IO.File.Exists(mPath))
                            {
                                //System.IO.Path.ChangeExtension(this.mPath, "");
                                System.IO.File.Move(this.mPath, this.mPath.Replace(@".vdisable", ""));
                                this.mEnabled = true;
                                this.mPath = this.mPath.Replace(".vdisable", "");
                                
                            }
                            else
                            {
                                throw new FileNotFoundException(this.Name);
                            }
                        }
                        break;
                    case false:
                        if (this.mEnabled)
                        {
                            if (System.IO.File.Exists(mPath))
                            {
                                //System.IO.Path.ChangeExtension(this.mPath, ".package.vdisable");
                                System.IO.File.Move(this.mPath, this.mPath + @".vdisable");
                                this.mEnabled = false;
                                this.mPath = this.mPath + ".vdisable";
                            }
                            else
                            {
                                throw new FileNotFoundException(this.Name);
                            }
                            
                        }
                        
                        break;
                }
            }
        }


        /// <summary>
        /// Default constructor for View Item
        /// </summary>
        /// <param name="name">string name for the item</param>
        /// <param name="path">string path for the package</param>
        /// <param name="type">whether create-a-sim or object</param>
        /// <param name="img">image stream for the item</param>
        public ViewItem(string name = "Unknown", string path = "", TypeFlag type = TypeFlag.Object, Stream img = null, bool ic = false)
        {
            this.Name = name;
            this.mPath = path;
            this.Flag = type;
            this.ImageStream = img;
            this.IsCorrupted = ic;
            if (System.IO.Path.GetExtension(path).Contains(".vdisable"))
            {
                this.mEnabled = false;
            }
            else
            {
                this.mEnabled = true;
            }
        }

        
        


        #region Sub-Class

        /// <summary>
        /// Type flag for the view item type
        /// </summary>
        public enum TypeFlag : int
        {
            CASP = 0x01,
            Object = 0x02
        }
        #endregion


        public override string ToString()
        {
            System.IO.FileInfo info = new FileInfo(this.mPath);
            return info.Name;// +" " + this.Name;
        }
    }
}
